package com.wude.guild.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mc.developmentkit.utils.ToastUtil;
import com.mc.developmentkit.views.SimpleFooter;
import com.mc.developmentkit.views.SimpleHeader;
import com.mc.developmentkit.views.SpringView;
import com.umeng.analytics.MobclickAgent;
import com.wude.bean.GiftInfo;
import com.wude.bean.UserInfo;
import com.wude.guild.R;
import com.wude.guild.adapter.MyGiftListAdapter;
import com.wude.guild.servercore.mints.TitleBaseFragment;
import com.wude.guild.views.CustomSwipeListView;
import com.wude.http.HttpCom;
import com.wude.http.HttpUtils;
import com.wude.tools.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2017/11/23 0023.
 */
@SuppressLint("HandlerLeak")
public class MyGiftFragment extends TitleBaseFragment {
    @BindView(R.id.error_text)
    TextView errorText;
    @BindView(R.id.error_layout)
    RelativeLayout errorLayout;
    @BindView(R.id.listView_my_gift)
    CustomSwipeListView listViewMyGift;
    @BindView(R.id.springview)
    SpringView springview;
    private MyGiftListAdapter myGiftListAdapter;
    private int limit = 1;
    private UserInfo loginUser;
    List<GiftInfo> informationBeen = new ArrayList<>();
    private Unbinder unbinder;

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_my_gift, container, false);
    }


    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("MyGiftFragment");
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("MyGiftFragment");
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        unbinder = ButterKnife.bind(this, view);
        setHeaderTitle("我的礼包");
        myGiftListAdapter = new MyGiftListAdapter(this);
        listViewMyGift.setAdapter(myGiftListAdapter);
        springview.setType(SpringView.Type.FOLLOW);
        springview.setListener(onFreshListener);
        springview.setHeader(new SimpleHeader(getActivity()));
        springview.setFooter(new SimpleFooter(getActivity()));
        initdata();

//        listViewMyGift.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                Intent intent = new Intent(getActivity(), GameDetActivity.class);
//                intent.putExtra("id", informationBeen.get(i).id + "");
//                startActivity(intent);
//            }
//        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private void initdata() {
        informationBeen.clear();
        loginUser = Utils.getLoginUser();
        limit = 1;
        if (loginUser != null) {
            Map<String, String> map = new HashMap<String, String>();
            map.put("token", loginUser.token);
            map.put("p", limit + "");
            HttpCom.POST(handler, HttpCom.getMyGiftURL(), map, false);
        }
    }

    public void genxin(List a) {
        if (a.size() == 0) {
            errorLayout.setVisibility(View.VISIBLE);
            springview.setVisibility(View.GONE);
            errorText.setText("您还没有领取过礼包哦");
        }
    }

    SpringView.OnFreshListener onFreshListener = new SpringView.OnFreshListener() {
        @Override
        public void onRefresh() {
            initdata();
        }

        @Override
        public void onLoadmore() {
            onLoadMord();
        }
    };

    /**
     * 加载更多
     */
    private void onLoadMord() {
        informationBeen.clear();
        limit = limit + 1;
        if (loginUser != null) {
            Map<String, String> map = new HashMap<String, String>();
            map.put("token", loginUser.token);
            map.put("p", limit + "");
            HttpCom.POST(mhandler, HttpCom.getMyGiftURL(), map, false);
        }
    }


    /**
     * 刚进来请求
     */
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            springview.onFinishFreshAndLoad();
            switch (msg.what) {
                case 1:
                    List<GiftInfo> giftInfos = HttpUtils.DNSGiftJL(msg.obj.toString());
                    if (giftInfos != null) {
                        errorLayout.setVisibility(View.GONE);
                        informationBeen.addAll(giftInfos);
                        myGiftListAdapter.setList(giftInfos);
                    } else {
                        errorLayout.setVisibility(View.VISIBLE);
                        springview.setVisibility(View.GONE);
                        errorText.setText("您还没有领取过礼包哦");
                    }
                    break;
                case 2:
                    errorLayout.setVisibility(View.VISIBLE);
                    springview.setVisibility(View.GONE);
                    errorText.setText("您还没有领取过礼包哦");
                    break;
            }
        }
    };


    /**
     * 加载更多请求
     */
    Handler mhandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            springview.onFinishFreshAndLoad();
            switch (msg.what) {
                case 1:
                    List<GiftInfo> giftInfos = HttpUtils.DNSGiftJL(msg.obj.toString());
                    if (giftInfos != null) {
                        informationBeen.addAll(giftInfos);
                        myGiftListAdapter.setList(giftInfos);
                    } else {
                        ToastUtil.showToast("已经到底了~");
                    }
                    break;
                case 2:

                    break;
            }
        }
    };
}
